package com.igoal.utils;

import java.util.Random;

/**
 * Create a random String with numbers and letters
 * 
 */
public class RandomString {

	private static final char[] symbols = new char[62];
	private static Random random = new Random();

	static {
		for (int idx = 0; idx < 10; idx++) {
			symbols[idx] = (char) ('0' + idx);
		}
		for (int idx = 10; idx < 36; idx++) {
			symbols[idx] = (char) ('a' + idx - 10);
		}
		for (int idx = 36; idx < 62; idx++) {
			symbols[idx] = (char) ('A' + idx - 36);
		}
	}

	public static String generate(int length) {
		if (length < 1) {
			throw new IllegalArgumentException("length < 1: " + length);
		}

		char[] buf = new char[length];
		for (int idx = 0; idx < length; idx++) {
			buf[idx] = symbols[random.nextInt(symbols.length)];
		}
		return new String(buf);
	}

}
